#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_scalar_block_layout : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require

#include "../../../core/shader_binding_ID.h"
#include "../../../core/shader_std.h"
#include "../../../core/shader_taster_transform.h"
#include "../../../core/shader_taster_color.h"

#include "../../../core/shader_mesh.h"
#include "../../../core/shader_meshTranformProp.h"



layout(points, invocations = 3) in;
layout(points, max_vertices = 64) out;



layout(push_constant, scalar) uniform _push_constants {
	S_ShaderConst_TextAttr drawProp;
};

layout(location = 0) in uint vCol[];
layout(location = 1) in uint vInstance[];
//layout(location = 2) in uint vVertIndex[];

in gl_PerVertex {
	vec4  gl_Position;
	float gl_PointSize;
}gl_in[];


layout(location = 1) out GS_FS_VERTEX {
	vec4	vColor;
	vec2	vUV_Offset;
	float	vUV_Bound;
	float	vUV_z;

	int		gTexType;
};

//vec3 q_vert[4] = {vec3(0,0,0), vec3(0,1,0), vec3(1,1,0), vec3(1,0,0)};

void main(void) {
	mat4 modelMat = mat_Model[drawProp.MatrixOffset + vInstance[0]];
	
	R_GlobalParameter_TranformProp meshTranformProp = R_GlobalParameter_TranformProp(drawProp.m_Address_MeshPropOffset);
	S_MeshTranformProp prop = meshTranformProp.TranformPropPtr[0];

	R_GlobalParameter_iVec4 ivec4_GParam = R_GlobalParameter_iVec4(drawProp.m_Address_TextCoord);
	R_GlobalParameter_UI32 ui32_GParam = R_GlobalParameter_UI32(drawProp.m_Address_TextID);
	R_GlobalParameter_UVec2 uvec2_GParam = R_GlobalParameter_UVec2(drawProp.m_Address_TextID_Range);

	

	uvec2 textID_range = uvec2_GParam.uvec2Ptr[gl_PrimitiveIDIn];

	vec4 rootCoord = gl_in[0].gl_Position;
	vec4 panelCoord = rootCoord;
	
	rootCoord = mat_ProjecView[0] * mat_Model[drawProp.MatrixOffset + vInstance[0]] * rootCoord;
	panelCoord = rootCoord;
	panelCoord.x += drawProp.m_CoordOffset.x;
	panelCoord.y -= drawProp.m_CoordOffset.y;

	float proj = rootCoord.w;
	
	uint e = textID_range.y - 1;
	for ( uint i = 0; i < textID_range.y; ++i ) {
		uint textID = ui32_GParam.ui32Ptr[textID_range.x + i];
		ivec4 uv = ivec4_GParam.ivec4Ptr[textID];

		gl_Position = panelCoord;
		
		float weidth = 0;
		switch(gl_InvocationID){
			case 0:{
				float top = (uv.w >> 16);
				//gl_Position = mat_ProjecView[0] * mat_Model[drawProp.MatrixOffset + vInstance[0]] * rootCoord;
				gl_Position.y -= proj / (top) / (drawProp.fontSize - 2);
				
				vUV_Offset = uv.xy;
				vUV_z = uv.z;
				vUV_Bound = drawProp.fontSize;
				
				gTexType = 1;
				break;
			}

			case 1:
				gl_Position.y -= proj / 3 / (drawProp.fontSize - 2);
			case 2:{
				vUV_Offset = vec2(0);
				vUV_Bound = 1;

				if (e == i) {
					vUV_z = drawProp.m_R_BackgroundText;
				} else if(i == 0) {
					vUV_z = drawProp.m_L_BackgroundText;
				} else {
					vUV_z = 0;
				}

				gl_Position.z += 0.00001;
				
				gTexType = 0;
				break;
			}
		}
		

		weidth = uv.w & 0x0000ffff;
		panelCoord.x += proj / (drawProp.fontSize - 3 - weidth) / (drawProp.fontSize - 3);
		//panelCoord.x += proj / (drawProp.fontSize - 3 - weidth) / (drawProp.fontSize - 3);
		//panelCoord.x += proj / (weidth + 4);
		vColor = f_glsl_buildColor(vCol[0]);
		
		gl_PointSize = drawProp.fontSize;
		EmitVertex();
	}
	

	gTexType = 2;
	gl_Position = rootCoord;
	gl_PointSize = 5;
	vColor = vec4(1,0.5,0,1);
	EmitVertex();

	//m_TextCoord[0];
	EndPrimitive();
	//gl_Position = mat_ProjecView[0] * mat_Model[drawProp.MatrixOffset + vInstance[0]] * gl_in[0].gl_Position;
	//gl_PointSize = 100;
	//R_GlobalParameter_uVec2 vec2_GParam = R_GlobalParameter_uVec2(drawProp.m_Address_TextCoord);

}







